﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Collections;

namespace Licenta1.Shapes
{
    [Serializable]
    class Polygon : Shape
    {
        Expression parameters;
        public ArrayList lines;
        bool init = false;

        public Polygon(Context con, Expression param)
            : base(con)
        {
            parameters = param;
            lines = new ArrayList();
        }

        public Polygon(Context con, ArrayList liness)
            : base(con)
        {
            lines = liness;
            init = true;
        }

        private void CheckParameters()
        {
            lines.Clear();
            try
            {
                ParameterList p_list = (ParameterList)parameters;

                FillLinesList(p_list);
            }
            catch (Exception e)
            {
                Console.WriteLine("Ex: " + e);
            }
            init = false;
            //Console.WriteLine("Size: " + lines.Count);
        }

        private void FillLinesList(ParameterList lst)
        {
            Line o = null;
            if (lst != null)
            {
                try
                {
                    ParameterList list = lst;
                    o = (Line)lst.FirstValue;

                    if (o != null)
                        lines.Add(o);

                    if (lst.SecondValue.GetType() == typeof(ParameterList))
                    {
                        ParameterList pl = (ParameterList)(lst.SecondValue);
                        FillLinesList(pl);
                    }
                    else if (lst.SecondValue.GetType() == typeof(Line))
                    {
                        Line o1 = (Line)(lst.SecondValue);

                        if (o1 != null)
                            lines.Add(o1);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("From..:" + e.ToString());
                }
            }
        }

        public override string Display()
        {
            if (!init)
                CheckParameters();
            init = true;

            string result = "";

            foreach (Line l in lines)
                result += "\n" + l.Display();

            return result;
        }

        public override object Value
        {
            get
            {
                if (!init)
                    CheckParameters();
                return this;
            }
        }

        public override void Draw()
        {
            if (!init)
                CheckParameters();

            //context.mp.objectsList.Add(this.GetClone());
            context.mp.objectsList.Add(new TinyRaytracer.MainParsing.ObjectToDraw("polygon", (Shape)this.GetClone(), context.activeColor, context.activePen));
        }

        public override object GetClone()
        {
            ArrayList ls = new ArrayList();
            foreach (Line l in lines)
                ls.Add(l.GetClone());

            return new Polygon(context, ls);
        }

        


    }
}